package com.bauer.springboot.consumerribbon.service.impl;

import com.bauer.springboot.consumerribbon.service.HelloService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloServiceImpl implements HelloService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private TaskExecutor testConnectionExecutor;

    @Override
    @HystrixCommand(fallbackMethod = "hiError")
    public void sayHello() {
        System.out.println();
    }

    @Override
    public void testConnection() {
        for (int i = 0; i < 40; i++) {
            testConnectionExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        restTemplate.getForObject("http://service-hi/test", String.class);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }
            });
        }
    }

    public void hiError() {
        System.out.println("hiError");
    }
}
